<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>底部导航</title>
    <link rel="stylesheet" type="text/css" href="./css/api.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />

</head>
<div class="header-d"></div>



</html>
<script type="text/javascript" src="script/api.js"></script>
<script type="text/javascript">
    //行政/事业(参公/行政支持/公益/经营)
    // SELECT COUNT(I44) FROM base_info WHERE I30
    function dele() {
        var db = api.require('CPDB');
        db.executeSql({
            name: 'test',
            sql: ''
        }, function(ret, err) {
            if (ret.status) {
                alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    }


    function removedata() {
        var fs = api.require('fs');
        fs.remove({
            path: 'file:///storage/emulated/0/ader/sql/data'
        }, function(ret, err) {
            if (ret.status) {
                // alert(JSON.stringify(ret));
                $api.setStorage('userData', null);
            } else {
                // alert(JSON.stringify(err));
            }
        });
    }

    function removebox() {
        var fs = api.require('fs');
        fs.remove({
            path: 'box://text'
        }, function(ret, err) {

            if (ret.status) {

                changeData();
                $api.setStorage('userData', null);
            } else {
                changeData();
                $api.setStorage('userData', null);
                // alert(JSON.stringify(err));
            }
        });
    }

    function removetext() {
        var fs = api.require('fs');
        fs.remove({
            path: 'file:///storage/emulated/0/ader/sql/text'
        }, function(ret, err) {

            if (ret.status) {

            } else {

                // alert(JSON.stringify(err));
            }
        });
    }



    function copy() {
        var fs = api.require('fs');
        fs.copyTo({
            oldPath: 'file:///storage/emulated/0/ader/sql/text',
            newPath: 'box://'
        }, function(ret, err) {
            if (ret.status) {
                // removetext();
                openDatabase();

            } else {
                alert(JSON.stringify(err));
                // openDatabase();
            }
        });
    }
    var res;

    function opendb() {
        var fs = api.require('fs');
        fs.open({
            path: 'box://sql/data',
            flags: 'read_write'
        }, function(ret, err) {
            if (ret.status) {
                res = ret.fd;
                //-----------------------------读取文件
                fs.read({
                    fd: ret.fd,
                    offset: 0,
                    length: 0
                }, function(ret, err) {
                    if (ret.status) {
                        alert(JSON.stringify(ret));
                        // var data = ret.data;
                        // excSql(data);
                        // createTable(data);

                    } else {
                        alert(JSON.stringify(err));
                    }
                });
                //-------
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function excSql(data) {
        var db = api.require('CPDB');
        arr = data.split(';');
        // alert(data);
        for (var i = 0; i < arr.length; i++) {
            db.executeSql({
                name: 'test',
                sql: arr[i]
            }, function(ret, err) {
                if (ret.status) {
                    // alert(JSON.stringify(ret));
                } else {
                    alert(JSON.stringify(err));
                }
            });
        }
    }


    function createTable(data) {
        var db = api.require('CPDB');
        // alert(data);
        db.executeSql({
            name: 'test',
            sql: data
        }, function(ret, err) {
            if (ret.status) {
                // alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });

    }


    function is_existence() {
        // console.log('is_existence');
        var fs = api.require('fs');
        fs.exist({
            path: 'file:///storage/emulated/0/ader/sql/data'
        }, function(ret, err) {
            if (ret.exist) {
                if (ret.directory) {
                    console.log('文件夹');
                } else {
                    console.log('文件');
                    removetext();
                    removebox();
                }
            } else {
                // alert('is_existence没有新文件');
                console.log('is_existence没有新文件');
                openDatabase();
            }
        });

    }

    function openDatabase() {
      var deviceId = api.deviceId;
        var db = api.require('CPDB');
        db.openDatabase({
            name: 'data',
            path: 'box://text',
            // path:'file:///storage/emulated/0/ader/sql/text',
            password: deviceId
                // path: 'widget://res/data',
        }, function(ret, err) {
            if (ret.status) {
                // alert(JSON.stringify(ret));
                open();
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function readDir() {
        var fs = api.require('fs');
        fs.readDir({
            path: 'box://'
                // path:'file:///storage/emulated/0/ader/sql/'
        }, function(ret, err) {
            if (ret.status) {
                alert(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    function removeboxCS() {
        var fs = api.require('fs');
        fs.remove({
            path: 'box://text'
        }, function(ret, err) {

            if (ret.status) {
              alert(JSON.stringify(ret));

            } else {
                alert(JSON.stringify(err));
            }
        });
    }




    apiready = function() {
      //
        //  removeboxCS();
        // readDir();
        is_existence();
        // openDatabase();
        // changeData();


    }


    function open() {

        api.openWin({
            name: 'login',
            url: './login.html',
        });


    }


    function changeData() {
        //
        var cpdb = api.require('CPDB');
        var deviceId = api.deviceId;
        cpdb.DBEncryptN({
            spath: 'file:///storage/emulated/0/ader/sql/data',
            tpath: 'file:///storage/emulated/0/ader/sql/text',
            // tpath: 'box://text',
            password: deviceId
        }, function(ret, err) {
            if (ret.status) {
                copy();
                removedata();
                removetext();
                // openDatabase();

            } else {
                // console.log('123');
                // alert(JSON.stringify(err));
                console.log(JSON.stringify(err));

            }
        });


    }






    function ceshi() {

        var db = api.require('CPDB');
        db.openDatabase({
            name: 'data',
            path: 'file:///storage/emulated/0/ader/sql/text',
            password: '123456'
        }, function(ret, err) {
            if (ret.status) {
                console.log(JSON.stringify(ret));
            } else {
                alert(JSON.stringify(err));
            }
        });

        //     cpdb.selectSql({
        //     name: 'data',
        //     sql: 'SELECT * FROM Base_Level',
        //     // password:'123456'
        //
        // }, function(ret, err) {
        //     if (ret.status) {
        //         // alert(JSON.stringify(ret));
        //       console.log(JSON.stringify(ret));
        //     } else {
        //       console.log(JSON.stringify(err));
        //     }
        // });








    }
</script>
